﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ProgNetComponents.Design.Attributes;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing.Design;
using ProgNetComponents.Design.Editors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using ProgNetComponentsDx.Base.Classes;
using ProgNetComponentsDx.Language;
using ProgNetComponentsDx.Enums;
using System.Windows.Forms;
using DevExpress.Utils;

namespace ProgNetComponentsDx.Mode
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class FormModeSettings : BaseSettings
    {
        private List<BaseEdit> _DependendControls = new List<BaseEdit>();

        private RequiredFieldValidationStruct _ValidationSettings;
        public FormModeSettings(object target, ModeProvider provider) :
            base(target, provider)
        {
            if (target is BaseEdit)
                _ValidationSettings = new RequiredFieldValidationStruct(provider, target as BaseEdit);
            else if (target is GridColumn)
                _ValidationSettings = new RequiredFieldValidationStruct(provider, target as GridColumn);
            else
                _ValidationSettings = new RequiredFieldValidationStruct(provider);
        }

        #region Is Browasble methods
        private bool IsReadOnlyModesBrowsable()
        {
            return (!(Owner is BarSubItem)) &&
                (Owner is GridColumn ||
                Owner is BaseEdit ||
                Owner is Control ||
                Owner is BarItem);
        }
        #endregion

        private FormMode _ReadOnlyModes = FormMode.Read | FormMode.View;
        private FormMode _RequiredModes = FormMode.None;
        private FormMode _AllowNewRecordModes = FormMode.Edit | FormMode.New;
        private FormMode _AllowDeleteModes = FormMode.Edit | FormMode.New;
        private bool _TreatNewRecordAsNewMode;
        private NewItemRowPosition _NewItemRowPosition;

        private AppearanceObject _DefaultAppearance;
        [Browsable(false),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public AppearanceObject DefaultAppearance
        {
            get
            {
                return _DefaultAppearance;
            }
        }

        public void SetDefaultAppearance(AppearanceObject obj)
        {
            if (_DefaultAppearance == null)
                _DefaultAppearance = new AppearanceObject();
            _DefaultAppearance.Assign(obj);
        }

        [BrowsableByMethod("IsReadOnlyModesBrowsable")]
        [RefreshProperties(RefreshProperties.All)]
        [Editor(typeof(FlagEnumUIEditor), typeof(UITypeEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        [AlwaysSerialize]
        [DefaultValue(FormMode.Read | FormMode.View)]
        public FormMode ReadOnlyModes
        {
            get { return _ReadOnlyModes; }
            set
            {
                _ReadOnlyModes = value;
                OnSettingsChanged();
            }
        }

        [BrowsableByPropertyValueType("Owner", typeof(GridColumn))]
        [DefaultValue(false)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public bool FullReadOnly
        {
            get;
            set;
        }

        [BrowsableByPropertyValueType("Owner",
            typeof(GridColumn),
            typeof(BaseEdit))]
        [RefreshProperties(RefreshProperties.All)]
        [Editor(typeof(FlagEnumUIEditor), typeof(UITypeEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        [AlwaysSerialize]
        [DefaultValue(FormMode.None)]
        public FormMode RequiredModes
        {
            get { return _RequiredModes; }
            set
            {
                if (_RequiredModes != value)
                {
                    _RequiredModes = value;
                    OnSettingsChanged();
                }
            }
        }

        #region GridView settings
        [BrowsableByPropertyValueType("Owner", typeof(GridView))]
        [RefreshProperties(RefreshProperties.All)]
        [Editor(typeof(FlagEnumUIEditor), typeof(UITypeEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        [DefaultValue(FormMode.Edit | FormMode.New)]
        public FormMode AllowDeleteModes
        {
            get { return _AllowDeleteModes; }
            set
            {
                if (_AllowDeleteModes != value)
                {
                    _AllowDeleteModes = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByPropertyValueType("Owner", typeof(GridView))]
        [RefreshProperties(RefreshProperties.All)]
        [Editor(typeof(FlagEnumUIEditor), typeof(UITypeEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        [DefaultValue(FormMode.Edit | FormMode.New)]
        public FormMode AllowNewRecordModes
        {
            get { return _AllowNewRecordModes; }
            set
            {
                if (_AllowNewRecordModes != value)
                {
                    _AllowNewRecordModes = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByPropertyValueType("Owner", typeof(GridView))]
        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public bool TreatNewRecordAsNewMode
        {
            get { return _TreatNewRecordAsNewMode; }
            set
            {
                if (_TreatNewRecordAsNewMode != value)
                {
                    _TreatNewRecordAsNewMode = value;
                    OnSettingsChanged();
                }
            }
        }

        [BrowsableByPropertyValueType("Owner", typeof(GridView))]
        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public NewItemRowPosition NewItemRowPosition
        {
            get { return _NewItemRowPosition; }
            set
            {
                if (_NewItemRowPosition != value)
                {
                    _NewItemRowPosition = value;
                    OnSettingsChanged();
                }
            }
        }
        #endregion

        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        [Category("Dependend Controls")]
        public List<BaseEdit> DependendControls
        {
            get { return _DependendControls; }
        }

        [DesignOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Category("Dependend Controls")]
        public BaseEdit AddDependendControl
        {
            get
            {
                if (_DependendControls.Count > 0)
                    return _DependendControls[_DependendControls.Count - 1];
                return null;
            }
            set
            {
                if (value != null && !_DependendControls.Contains(value))
                    _DependendControls.Add(value);
            }
        }

        #region ValidationSettings
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [BrowsableByPropertyValueType("Owner", typeof(BaseEdit), typeof(GridColumn))]
        [System.ComponentModel.NotifyParentProperty(true)]
        public RequiredFieldValidationStruct ValidationSettings
        {
            get { return _ValidationSettings; }
        }
        #endregion
    }
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class RequiredFieldValidationStruct : ProgNetComponents.Classes.BaseCustomObject
    {
        public override string ToString()
        {
            return "ValidationSettings";
        }
        private ModeProvider provider;
        private BaseEdit Edit;
        private GridColumn GridColumn;

        private string _FullValidationError;
        private string _LngFieldName;

        internal RequiredFieldValidationStruct(ModeProvider provider)
        {
            this.provider = provider;
        }
        public RequiredFieldValidationStruct(ModeProvider provider, BaseEdit edit)
        {
            this.provider = provider;
            this.Edit = edit;
        }
        public RequiredFieldValidationStruct(ModeProvider provider, GridColumn column)
        {
            this.provider = provider;
            this.GridColumn = column;
        }

        #region Is Browsable methods
        private bool IsLngErrorSettings()
        {
            return UseLngSettings && SetRequiredError;
        }
        #endregion

        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public bool SetRequiredError
        {
            get;
            set;
        }

        [BrowsableByPropertyValue("SetRequiredError", true)]
        [RefreshProperties(RefreshProperties.All)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public bool UseLngSettings
        {
            get;
            set;
        }

        [BrowsableByMethod("IsLngErrorSettings")]
        [UIEditor(typeof(ColumnNameEditor))]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string LngFieldName
        {
            get
            {
                if (_LngFieldName.IsNull() && UseLngSettings)
                {
                    if (Edit != null && Edit.DataBindings["EditValue"] != null)
                        return Edit.DataBindings["EditValue"].BindingMemberInfo.BindingField;
                    else if (GridColumn != null)
                        return GridColumn.FieldName;
                }
                return _LngFieldName;
            }
            set { _LngFieldName = value; }
        }

        [BrowsableByMethod("IsLngErrorSettings")]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string LngManager
        {
            get;
            set;
        }

        [BrowsableByPropertyValue("SetRequiredError", true)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string FieldCaption
        {
            get;
            set;
        }


        [BrowsableByPropertyValue("SetRequiredError", true)]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string FullValidationError
        {
            get
            {
                if (_FullValidationError.IsNotNull() || provider.FullValidationError.IsNull())
                    return _FullValidationError;
                return provider.FullValidationError;
            }
            set { _FullValidationError = value; }
        }

        [BrowsableByMethod("IsLngErrorSettings")]
        [System.ComponentModel.NotifyParentProperty(true)]
        public string LngFullValidationError
        {
            get;
            set;
        }

        public string GetFormatedError()
        {
            string frmt = (UseLngSettings ? LngFullValidationError : FullValidationError);
            if (frmt.IsNull())
                frmt = FullValidationError;

            string fldName = FieldCaption;
            if (fldName.IsNull() && LngFieldName.IsNotNull())
                fldName = ProgNetLanguage.sGetStringFromMgr(LngManager, LngFieldName);
            if (fldName.IsNull() && GridColumn.IsNotNull())
                fldName = GridColumn.GetCaption();
            return frmt.Replace("{0}", fldName);
        }
    }

    
}

